const gulp = require('gulp');
const path = require('path');
const $ = require('gulp-load-plugins')();
const del = require('del');
const config = require('../config/data');
const uglifyEs = require('uglify-es');
const composer = require('gulp-uglify/composer');
const uglify = composer(uglifyEs, console);

let distDir = config.dirs.dist;
const devDir = config.dirs.dev;
let unInDirs = config.dirs.unInDirs.map(function (str) {
  return '!' + str;
});
$.htmlMinFun = function (htmlDirs, condition) {
  if (!condition) {
    condition = function () {
      return true;
    };
  }
  return gulp.src(htmlDirs, {base: devDir})
    .pipe($.if(condition, $.htmlmin(config.htmlMinOptions)))
    .pipe(gulp.dest(distDir));
};


//删除dist文件夹
gulp.task('release:clear', function () {
  return del(distDir);
});

gulp.task('release:script', function () {
  const condition = function (f) {
    if (f.path.endsWith('.min.js')) {
      return false;
    }
    return true;
  };
  return gulp.src(distDir + '/**/*.js')
    .pipe($.if(condition, uglify()))
    .pipe(gulp.dest(distDir));
});

gulp.task('release:htmlmin', function () {
  const condition = function (f) {
    if (f.path.indexOf(path.sep + 'fonts' + path.sep) != -1) {
      return false;
    }
    return true;
  };
  let htmlMinDirs = [devDir + '/**/*.html'].concat(unInDirs);
  return $.htmlMinFun(htmlMinDirs, condition);
});
